*** Generating figure 3: Baseline results for the full sample ***

** Preamble **

clear all
set more off

local path_input  "C:/Users/$user/Dropbox/$project/master/output/data"
local path_output "C:/Users/$user/Dropbox/$project/master/output/tables"


* Note: local directory is the output folder 
cd "`path_output'"

					** Loading the input file **


use `path_input'/dataset_final_may20, clear

estimates clear

xtset codigo_ibge turno

local baseline_controls "pop1991 years_sch1991 renda1991 sh_tv1991 sh_agua1991 sh_elect1991 sh_rural"
local CGB_FEs			"pct_sh_tv1991 pct_sh_rural pct_renda1991"

gen votos_pc=total_/pop1991
gen non_valid=sh_Tbrancos_+sh_Tnulos_

/*generating percentiles*/
foreach var in `baseline_controls' {
xtile pct_`var'=  `var', n(10)
replace pct_`var'=0 if turno==2
}

/*generating pscores*/
encode uf, gen(UF)
psmatch2 Globo T1* if turno==1,  noreplace neighbor(1)
egen ppp=mean(_pscore), by(codigo)
gen pscore=1
replace pscore=ppp/(1-ppp) if Globo==0

label var treatment "Globo"
label var treatment1 "Globo and Others"
label var treatment2 "Globo Only"

/*make margin for charming regressors labels */
foreach v of varlist * {

	label variable `v' `"\hspace{0.1cm} `: variable label `v''"'
	
}


							** Table: Main results, Other electoral outcomes **							


/*
The table reports regression estimates of the effect of Globo's edited coverage on the share of non-valid votes and turnout. All specifications control for municipality fixed effects vote shares. Columns (1) and (2) reports baseline estimates on the share of non-valid votes. Column (2) splits treatment status according to coverage of other TV broadcasters. Columns (3) and (4) repeat columns' (1) and (2) specification and estimate the effect of the edited coverage on turnout. In Panel A, we control for group fixed effects. To create group fixed effects, we split municipalities into deciles according to the share of households
with TV, the share of population living in rural areas, and the income per capita. In Panel B, untreated units are weighted by inverse propensity scores to create a control group with similar pretreatment characteristics (Abadie, 2005). Heteroskedasticity-adjusted standard errors clustered at the municipality level are reported in brackets. Significantly different from zero at $99\% (***)$, $95\% (**)$, and $90\% (*)$ confidence level.
*/

local legend "The table reports regression estimates of the effect of Globo's edited coverage on the share of non-valid votes and turnout. All specifications control for municipality fixed effects, time fixed effects, and first round vote shares interacted with time fixed effects. Columns (1) and (2) reports baseline estimates on the share of non-valid votes. Column (2) splits treatment status according to coverage of other TV broadcasters. Columns (3) and (4) repeat columns' (1) and (2) specification and estimate the effect of the edited coverage on turnout. In Panel A, we control for group fixed effects. To create group fixed effects, we split municipalities into deciles according to the share of householdswith TV, the share of population living in rural areas, and the income per capita. In Panel B, untreated units are weighted by inverse propensity scores to create a control group with similar pretreatment characteristics (Abadie, 2005). Heteroskedasticity-adjusted standard errors clustered at the municipality level are reported in brackets. Significantly different from zero at $99\% (***)$, $95\% (**)$, and $ 90\% (*)$ confidence level."

									   ** Panel A **									
							

eststo: xi: reghdfe non_valid treatment 			sh_Tpsp_-sh_Tpsdb_ turno, vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')
estadd local Pvalue "-"
estadd local PartiesControl "Yes"

eststo: xi: reghdfe non_valid treatment1 treatment2 sh_Tpsp_-sh_Tpsdb_ turno, vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')
test treatment1 - treatment2 = 0
scalar Pvalue = round(r(p), 0.001)
estadd scalar Pvalue
estadd local PartiesControl "Yes"

eststo: xi: reghdfe votos_pc treatment 		        sh_Tpsp_-sh_Tpsdb_ turno, vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')
estadd local Pvalue "-"
estadd local PartiesControl "Yes"

eststo: xi: reghdfe votos_pc treatment1 treatment2  sh_Tpsp_-sh_Tpsdb_ turno, vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')
test treatment1 - treatment2 = 0
scalar Pvalue = round(r(p), 0.001)
estadd scalar Pvalue
estadd local PartiesControl "Yes"


										** Panel B **
									
									
eststo: xi: reghdfe non_valid  treatment 			 sh_Tpsp_-sh_Tpsdb_ turno [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)
estadd local Pvalue "-"
estadd local PartiesControl "Yes"

eststo: xi: reghdfe non_valid  treatment1 treatment2 sh_Tpsp_-sh_Tpsdb_ turno [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)
test treatment1 - treatment2 = 0
scalar Pvalue = round(r(p), 0.001)
estadd scalar Pvalue
estadd local PartiesControl "Yes"

eststo: xi: reghdfe votos_pc treatment 		         sh_Tpsp_-sh_Tpsdb_ turno [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)
estadd local Pvalue "-"
estadd local PartiesControl "Yes"

eststo: xi: reghdfe votos_pc treatment1 treatment2   sh_Tpsp_-sh_Tpsdb_ turno [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)
test treatment1 - treatment2 = 0
scalar Pvalue = round(r(p), 0.001)
estadd scalar Pvalue
estadd local PartiesControl "Yes"
													
													
										*** Producing tables ***
								
								
esttab est1 est2 est3 est4 using Table_baseline_invalid_turnout_panel_A.tex, ///
keep(treatment treatment1 treatment2) ///
order(treatment treatment1 treatment2) ///
stat(N r2 Pvalue PartiesControl, fmt(0 2 2 0) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{S}{@}" "\multicolumn{1}{S}{@}" "\multicolumn{1}{c}{@}") ///
label("Observations" "R-squared" "Coeff. equality (\emph{p-value})" "Electoral controls * Round")) ///
mgroups("Non-valid votes" "Turnout", pattern(1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cline{@span})) ///
star(* 0.10 ** 0.05 *** 0.01) ///
nomtitles  ///
r2 b(3) replace  se(3) brac compress nonotes label  nogaps staraux ///
nonotes addnotes("\begin{minipage}{.75\linewidth} \footnotesize \smallskip \textbf{Note:} `legend' \end{minipage}" )


esttab est5 est6 est7 est8 using Table_baseline_invalid_turnout_panel_B.tex, ///
keep(treatment treatment1 treatment2) ///
order(treatment treatment1 treatment2) ///
stat(N r2 Pvalue PartiesControl, fmt(0 2 2 0) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{S}{@}" "\multicolumn{1}{S}{@}" "\multicolumn{1}{c}{@}") ///
label("Observations" "R-squared" "Coeff. equality (\emph{p-value})" "Electoral controls * Round")) ///
mgroups("Non-valid votes" "Turnout", pattern(1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cline{@span})) ///
star(* 0.10 ** 0.05 *** 0.01) ///
nomtitles  ///
r2 b(3) replace  se(3) brac compress nonotes label  nogaps staraux ///
nonotes addnotes("\begin{minipage}{.75\linewidth} \footnotesize \smallskip \textbf{Note:} `legend' \end{minipage}" )

include "https://raw.githubusercontent.com/steveofconnell/PanelCombine/master/PanelCombine.do"

panelcombine, use(Table_baseline_invalid_turnout_panel_A.tex Table_baseline_invalid_turnout_panel_B.tex) ///
paneltitles("Group FEs" "Reweighted") columncount(5) save(baseline_results_Blank_Null.tex) cleanup

